package de.karlNet.gifSocket.controllers;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;


import de.karlNet.gifSocket.controllers.interfaces.ISessionController;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

public class SessionController implements ISessionController {
	private ByteArrayOutputStream myBAOS = new ByteArrayOutputStream();
	
	private final String serverUrl;
	private final Robot robot;

	public SessionController(final String serverUrl) throws AWTException {
		this.serverUrl = serverUrl;
		this.robot = new Robot();
	}

	public void joinSession(int sessionNumber, OutputStream client) {
	}

	public void addPictureToSession(int sessionNumber, BufferedImage imageToAdd)
			throws ClientProtocolException, IOException {
                        

                ByteArrayOutputStream bos = new ByteArrayOutputStream(0);
                ImageOutputStream ios = ImageIO.createImageOutputStream(bos);
                ImageIO.write(imageToAdd, "jpeg", ios);
                
                
                
	
	

		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httppost = new HttpPost(this.serverUrl
				+ "/gif/upload?session_id=" + sessionNumber);

		MultipartEntity mpEntity = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
                
		InputStreamBody uploadFilePart = new InputStreamBody(new ByteArrayInputStream(bos.toByteArray()),
				"application/octet-stream", "test.file");
		mpEntity.addPart("file", uploadFilePart);
		httppost.setEntity(mpEntity);

		HttpResponse response = httpclient.execute(httppost);
	}

	public String createSession(int x, int y) throws ClientProtocolException,
			IOException {

		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpGet httpGet = new HttpGet(this.serverUrl + "/gif/session/create?x="
				+ x + "&y=" + y);

		HttpResponse response;
		response = httpclient.execute(httpGet);
		String responseString = EntityUtils.toString(response.getEntity(),
				"UTF-8");
		return responseString;

	}

	public void addMouseListener(int sessionID, OutputStream outputStream)
			throws ClientProtocolException, IOException {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(this.serverUrl
				+ "/gif/register_click_client?session_id=" + sessionID);

		HttpResponse response = httpclient.execute(httpGet);
		InputStream stream = response.getEntity().getContent();
		int readByte = -1;
		int leftOrRight = -1;
		do {
			leftOrRight = stream.read();

			do {
				readByte = stream.read();
			} while (readByte != 0);
			if (leftOrRight == 1) {
				this.click(sessionID, 0, 0);
			}
			if (leftOrRight == 2) {
				this.rightClick(sessionID, 0, 0);
			}
		} while (readByte != -1);
		System.out.println("finished reading");
	}

	public void rightClick(int sessionID, int i, int j) throws ClientProtocolException, IOException {

	}

	public void click(int sessionID, Integer x, Integer y) throws IOException {
		this.robot.mousePress(InputEvent.BUTTON1_MASK);
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.robot.mouseRelease(InputEvent.BUTTON1_MASK);
	}
}
